package com.cqjtu.eecs.service.impl;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    /**
     * set redis: string类型
     * @param key key
     * @param value value
     */
    public void setString(String key, String value){
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(key, value);
    }
    /**
     * get redis: string类型
     * @param key key
     * @return 返回对应key的value
     */
    public String getString(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * setex 设置k,v 并指定过期时间
     * @param key
     * @param value
     * @param second 时间，秒
     */
    public void setEXString(String key, String value, Long second){
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(key, value, second, TimeUnit.SECONDS); // 设置k,v同时设置过期时间
    }

    /**
     * 删除某个key
     * @param key
     * @return
     */
    public boolean remove(String key){
        return stringRedisTemplate.delete(key);
    }
}
